#include "chooselevelscene.h"
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QPainter>
#include <QTimer>
#include <QLabel>
#include <QDebug>
#include <QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{

    //菜单栏
    QMenuBar *bar = menuBar();

    QMenu * startMenu = bar->addMenu("开始");

    //菜单项
    QAction *quitAction = startMenu->addAction("退出");

    connect(quitAction,&QAction::triggered,this,[=](){
        this->close();
    });

    //设置标题
    this->setWindowTitle("选择关卡场景");
    this->setFixedSize(320,580);
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //选中音效
    QSound * chooseSound = new QSound(":/res/TapButtonSound.wav");
    QSound * backSound = new QSound(":/res/BackButtonSound.wav");

    //返回按钮
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,this,[=](){

        //延时发送返回信号
        // QTimer::singleShot(200,this,[=](){
        //     emit this->chooseSceneBack();
        // });
        backSound->play();
        emit this->chooseSceneBack();
    });


    //创建20个选择关卡的按钮
    for (int i = 0; i < 20; ++i) {
        MyPushButton *menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25 + (i%4) * 70,(i/4) * 70 + 130 );

        connect(menuBtn,&MyPushButton::clicked,this,[=](){
            qDebug() << "您选择的关卡是第" << i+1 << "关";

            chooseSound->play();
            playScene = new PlayScene(i+1);
            this->hide();

            //设置游戏场景的位置
            this->playScene->setGeometry(this->geometry());
            this->playScene->show();

            //监听游戏场景中点击返回按钮
            connect(this->playScene,&PlayScene::chooseSceneBack,this,[=](){

                this->setGeometry(this->playScene->geometry());
                delete this->playScene;
                this->playScene = NULL;
                this->show();
            });
        });
        //创建显示具体的关卡号的label
        QLabel *label = new QLabel;
        label->setParent(this);

        label->setFixedSize(menuBtn->width(),menuBtn->height());

        label->setText(QString::number(i+1));
        label->move(25 + (i%4) * 70,(i/4) * 70 + 130 );

        //设置居中
        label->setAlignment(Qt::AlignCenter);


        //设置鼠标的穿透事件
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }

}

void ChooseLevelScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;

    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    //pix = pix.scaled(pix.width() * 0.5,pix.height()*0.5);

    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);


}
